﻿Imports Microsoft.VisualBasic
Imports System.Collections.Specialized
Imports System.Text
Imports System.Web.HttpUtility

Namespace roncliProductions.LibWowArmory

    ''' <summary>
    ''' A helper class to create a URL with a query string
    ''' </summary>
    ''' <remarks></remarks>
    Friend Class URLBuilder
        Inherits NameValueCollection

        Private strBaseURL As String

        ''' <summary>
        ''' Creates a new URL Builder object with a base URL
        ''' </summary>
        ''' <param name="strBaseURL"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strBaseURL As String)
            Me.strBaseURL = strBaseURL
        End Sub

        ''' <summary>
        ''' Returns the URL complete with the querystring
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Function ToString() As String
            Dim sbString As New StringBuilder()

            For Each strKey As String In MyBase.Keys
                If Not String.IsNullOrEmpty(MyBase.Item(strKey)) Then
                    For Each strValue In MyBase.Item(strKey).Split(CChar(","))
                        sbString.Append(String.Format("{0}{1}={2}", IIf(sbString.Length = 0, "?", "&"), UrlEncodeUnicode(strKey), UrlEncodeUnicode(strValue)))
                    Next
                End If
            Next

            Return String.Format("{0}{1}", strBaseURL, sbString.ToString())
        End Function
    End Class

End Namespace
